package com.haois.mp.database.module.common.service.impl;


import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.haois.mp.database.module.common.daomain.UserToken;
import com.haois.mp.database.module.common.daomain.WxUser;
import com.haois.mp.database.module.miniapp.service.AppUserFollowService;
import com.haois.mp.database.module.offaccounts.service.UserFollowService;
import com.haois.mp.database.module.common.dao.MpUserTokenMapper;
import com.haois.mp.database.module.common.dao.UserTokenHisMapper;
import com.haois.mp.database.module.basic.dao.IBasicDao;
import com.haois.mp.database.module.basic.service.impl.BasicServiceImpl;
import com.haois.mp.database.commom.enums.MpTypeEnum;
import com.haois.mp.database.module.common.dao.MaUserTokenMapper;
import com.haois.mp.database.module.common.service.IUserTokenService;
import com.haois.mp.database.util.jx.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service("iUserTokenService")
@Transactional
public class UserTokenServiceImpl extends BasicServiceImpl<UserToken, String>
        implements IUserTokenService {

    @Autowired
    private MpUserTokenMapper mpUserTokenMapper;
    @Autowired
    private MaUserTokenMapper maUserTokenMapper;
    @Autowired
    private UserTokenHisMapper userTokenHisMapper;
    @Autowired
    private UserFollowService userFollowService;
    @Autowired
    private AppUserFollowService appUserFollowService;


    @Override
    public IBasicDao<UserToken, String> getDao() {
        return mpUserTokenMapper;
    }

    @Override
    public UserToken findByUserId(String userId, String sysType) {
        List<UserToken> userTokens = null;
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            userTokens = mpUserTokenMapper.findByUserId(userId);
        } else {
            userTokens = maUserTokenMapper.findByUserId(userId);
        }
        if (userTokens != null && userTokens.size() > 0) {
            return userTokens.get(0);
        } else {
            return null;
        }
    }

    @Override
    public UserToken findByOpenId(String openId, String sysType) {
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            return mpUserTokenMapper.findByOpenId(openId);
        } else {
            return maUserTokenMapper.findByOpenId(openId);
        }
    }

    @Override
    public UserToken findByAccessToken(String accessToken, String sysType) {
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            return mpUserTokenMapper.findByAccessToken(accessToken);
        } else {
            return maUserTokenMapper.findByAccessToken(accessToken);
        }
    }

    @Override
    public void deleteByAccessToken(String accessToken, String sysType) {
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            mpUserTokenMapper.deleteByAccessToken(accessToken);
        } else {
            maUserTokenMapper.deleteByAccessToken(accessToken);
        }
    }

    @Override
    public void deleteByOpenId(String openId, String sysType) {
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            mpUserTokenMapper.deleteByOpenId(openId);
        } else {
            maUserTokenMapper.deleteByOpenId(openId);
        }
    }

    @Override
    public void deleteByUserId(String userId, String sysType) {
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            mpUserTokenMapper.deleteByUserId(userId);
        } else {
            maUserTokenMapper.deleteByUserId(userId);
        }
    }

    @Override
    public void logout(String openid, String sysType, String reason) {
        UserToken userToken = this.findByOpenId(openid, sysType);
        if (userToken == null) {
            return;
        }
        if (sysType.equals(MpTypeEnum.OFFACCOUNTS.getType())) {
            userFollowService.updateUser(openid, null, reason);
        } else {
            appUserFollowService.updateUser(openid, null, reason);
        }
        this.deleteByOpenId(openid, sysType);
        this.saveUserTokenHis(userToken, reason, sysType);
    }

    @Override
    public void saveUserTokenHis(UserToken userToken, String reason, String sysType) {
        reason = StringUtils.isEmpty(reason) ? "主动退出登录" : reason;
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            userTokenHisMapper.insertMp(userToken, reason, "", DateUtil.getYMdHm());
        } else {
            userTokenHisMapper.insertMa(userToken, reason, "", DateUtil.getYMdHm());
        }
    }

    @Override
    public List<UserToken> findAllByAccessToken(String accessToken, String sysType) {
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            return mpUserTokenMapper.findAllByAccessToken(accessToken);
        } else {
            return maUserTokenMapper.findAllByAccessToken(accessToken);
        }
    }

    @Override
    public void login(UserToken userToken) {
        mpUserTokenMapper.insert(userToken);
        userFollowService.updateUser(userToken.getOpenId(), userToken.getUserId(), "登录");
    }

    @Override
    public void login(UserToken userToken, WxMaUserInfo maUserInfo) {
        maUserTokenMapper.insert(userToken);
//        appUserFollowService.updateUser(userToken.getOpenId(), userToken.getUserId(), "登录");
        WxUser wxUser = new WxUser(maUserInfo);
        wxUser.setUserId(userToken.getUserId());
        wxUser.setRemark("登录");
        wxUser.setOpenId(userToken.getOpenId());
        appUserFollowService.updateUser(wxUser);
        appUserFollowService.syncWxMpUser(wxUser);
    }


    @Override
    public void updateInfo(UserToken userToken) {
        mpUserTokenMapper.update(userToken);
        userFollowService.updateUser(userToken.getOpenId(), userToken.getUserId(), "更新登录");
    }

    @Override
    public void updateInfo(UserToken userToken, WxMaUserInfo maUserInfo) {
        maUserTokenMapper.update(userToken);
        WxUser wxUser = new WxUser(maUserInfo);
        wxUser.setUserId(userToken.getUserId());
        wxUser.setOpenId(userToken.getOpenId());
        wxUser.setRemark("更新登录");
        appUserFollowService.updateUser(wxUser);
        appUserFollowService.syncWxMpUser(wxUser);
    }

    @Override
    public List<UserToken> find(UserToken userToken) {
        return mpUserTokenMapper.query(new HashMap());
    }

}
